package com.gzcstec.ftp;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.pool2.impl.GenericObjectPool;

/**
 * ftp连接池配置
 */
public class FTPClientPool {

    private GenericObjectPool<FTPClient> ftpClientPool;

    private FTPClientPoolProperties ftpClientPoolProperties;

    /**
     * 初始化连接池
     */
    public FTPClientPool(FTPClientPoolProperties ftpClientPoolProperties){
       this.ftpClientPoolProperties = ftpClientPoolProperties;
       ftpClientPool = new GenericObjectPool(new FTPClientFactory(ftpClientPoolProperties), ftpClientPoolProperties);
    }

    /**
     * 获取连接
     * @return
     * @throws Exception
     */
    public FTPClient borrowObject() throws Exception {
        FTPClient ftpClient = ftpClientPool.borrowObject();
        ftpClient.changeWorkingDirectory(ftpClientPoolProperties.getWorkingDirectory());
        return ftpClient;
    }

    /**
     * 释放连接
     * @param ftpClient
     */
    public void returnObject(FTPClient ftpClient) {
        ftpClientPool.returnObject(ftpClient);
    }

    public FTPClientPoolProperties getFtpClientPoolProperties() {
        return ftpClientPoolProperties;
    }
}
